package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcCheckRecord;
import com.csun.cmny.provider.model.domain.OmcInStockRecord;
import com.csun.cmny.provider.model.domain.OmcInventory;
import com.csun.cmny.provider.model.dto.inventory.*;
import com.csun.cmny.provider.model.vo.inventory.CheckRecordVo;
import com.csun.cmny.provider.model.vo.inventory.InStockVo;
import com.csun.cmny.provider.model.vo.inventory.InventoryVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcCheckRecordService;
import com.csun.cmny.provider.service.OmcInStockRecordService;
import com.csun.cmny.provider.service.OmcInventoryService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/inventory", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcInventoryController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcInventoryService inventoryService;
    @Resource
    private OmcInStockRecordService stockRecordService;
    @Resource
    private OmcCheckRecordService checkRecordService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "入库新增")
    @RequestMapping(value = "/inStock", method = RequestMethod.POST)
    public Wrapper addOmcContract(@RequestBody @Valid InStockDto inStockDto, BindingResult br) throws ParseException {

        OmcInventory inventory = new OmcInventory();
        inventory.setArticleNo(inStockDto.getArticleNo());
        inventory.setGroupId(getLoginAuthDto().getGroupId());

        //判定此类型物品是否存在,如果存在就在原库存基础上增加
        List<OmcInventory> select = inventoryService.select(inventory);
        Long inventoryId=null;
        if(PublicUtil.isNotEmpty(select)){
            if(!select.get(0).getArticleName().equals(inStockDto.getArticleName())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("stock.articleName.error", null));
            }

            //判断此物品是否被盘点过
            OmcCheckRecord omcCheckRecord = new OmcCheckRecord();
            omcCheckRecord.setInventoryId(select.get(0).getId());
            List<OmcCheckRecord> omcCheckRecords = checkRecordService.select(omcCheckRecord);
            if(PublicUtil.isEmpty(omcCheckRecords)){
                inventory.setTheoryLibraryQuantity(select.get(0).getTheoryLibraryQuantity()+inStockDto.getQuantity());
            }
            inventoryId=select.get(0).getId();
            inventory.setId(select.get(0).getId());
            inventory.setLibraryQuantity(select.get(0).getLibraryQuantity()+inStockDto.getQuantity());


            inventoryService.update(inventory);
        }else {

            //如果不存在,就新增产品类别

            OmcInventory omcInventory = new OmcInventory();
            BeanUtils.copyProperties(inStockDto, omcInventory);
            omcInventory.setLibraryQuantity(inStockDto.getQuantity());
            omcInventory.setTheoryLibraryQuantity(inStockDto.getQuantity());
            omcInventory.setGroupId(getLoginAuthDto().getGroupId());
            omcInventory.setUserId(getLoginAuthDto().getUserId());
            omcInventory.setOperator(getLoginAuthDto().getUserName());
            int save = inventoryService.save(omcInventory);
            inventoryId=omcInventory.getId();

        }

        //新增入库记录
        OmcInStockRecord omcInStockRecord = new OmcInStockRecord();
        BeanUtils.copyProperties(inStockDto,omcInStockRecord);
        omcInStockRecord.setInventoryId(inventoryId);
        omcInStockRecord.setOnInStaus(0);
        omcInStockRecord.setGroupId(getLoginAuthDto().getGroupId());
        omcInStockRecord.setUserId(getLoginAuthDto().getUserId());
        omcInStockRecord.setOperator(getLoginAuthDto().getUserName());
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(inStockDto.getStockDate());
        omcInStockRecord.setStockDate(date);
        int relust = stockRecordService.save(omcInStockRecord);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询入库记录")
    @RequestMapping(value = "/selectInstock", method = RequestMethod.POST)
    public Wrapper selectInstock(@RequestBody @Valid InStockQueryDto inStockQueryDto, BindingResult br) throws ParseException {


        inStockQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<InStockVo> list=inventoryService.selectInstock(inStockQueryDto);

        for (InStockVo inStockVo : list) {
            inStockVo.setStockDate(inStockVo.getStockDate().substring(0,10));
        }
        PageInfo<InStockVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "出库新增")
    @RequestMapping(value = "/outStock", method = RequestMethod.POST)
    public Wrapper outStock(@RequestBody @Valid OutStockDto outStockDto, BindingResult br) throws ParseException {
        OmcInventory inventory = inventoryService.selectByKey(outStockDto.getInventoryId());

        OmcInventory omcInventory = new OmcInventory();

        //判断此物品是否被盘点过
        OmcCheckRecord omcCheckRecord = new OmcCheckRecord();
        omcCheckRecord.setInventoryId(outStockDto.getInventoryId());
        List<OmcCheckRecord> omcCheckRecords = checkRecordService.select(omcCheckRecord);

        if(PublicUtil.isEmpty(omcCheckRecords)){

            omcInventory.setTheoryLibraryQuantity(inventory.getTheoryLibraryQuantity()-outStockDto.getQuantity());
        }

        //更改库存数量
        if(inventory.getLibraryQuantity()<outStockDto.getQuantity()){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("stock.null.error", null));
        }

        omcInventory.setId(inventory.getId());
        omcInventory.setLibraryQuantity(inventory.getTheoryLibraryQuantity()-outStockDto.getQuantity());
        int update = inventoryService.update(omcInventory);

        //新增出库记录
        OmcInStockRecord omcInStockRecord = new OmcInStockRecord();
        BeanUtils.copyProperties(outStockDto,omcInStockRecord);
        omcInStockRecord.setInventoryId(omcInventory.getId());
        omcInStockRecord.setOnInStaus(1);
        omcInStockRecord.setGroupId(getLoginAuthDto().getGroupId());
        omcInStockRecord.setUserId(getLoginAuthDto().getUserId());
        omcInStockRecord.setOperator(getLoginAuthDto().getUserName());
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(outStockDto.getStockDate());
        omcInStockRecord.setStockDate(date);
        int relust = stockRecordService.save(omcInStockRecord);



        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询出库记录")
    @RequestMapping(value = "/selectOutstock", method = RequestMethod.POST)
    public Wrapper selectOutstock(@RequestBody @Valid InStockQueryDto inStockQueryDto, BindingResult br) throws ParseException {


        inStockQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<InStockVo> list=inventoryService.selectOutstock(inStockQueryDto);

        for (InStockVo inStockVo : list) {
            inStockVo.setStockDate(inStockVo.getStockDate().substring(0,10));
        }
        PageInfo<InStockVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询库存")
    @RequestMapping(value = "/selectInventory", method = RequestMethod.POST)
    public Wrapper selectInventory(@RequestBody @Valid InventoryQueryDto inventoryQueryDto, BindingResult br)   {

        inventoryQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<InventoryVo>list=inventoryService.selectInventory(inventoryQueryDto);
        PageInfo<InventoryVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除库存")
    @RequestMapping(value = "/deleteInventory", method = RequestMethod.POST)
    public Wrapper deleteInventory(Long id)   {

        OmcInventory omcInventory = inventoryService.selectByKey(id);
        if(omcInventory.getLibraryQuantity()!=0||omcInventory.getTheoryLibraryQuantity()!=0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("invent.quantity.error", null));
        }

        int delete = inventoryService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "盘点库存")
    @RequestMapping(value = "/takeStock", method = RequestMethod.POST)
    public Wrapper takeStock(@RequestBody @Valid TakeStock takeStock, BindingResult br) throws ParseException {

        OmcInventory omcInventory = new OmcInventory();

        BeanUtils.copyProperties(takeStock,omcInventory);

        OmcInventory inventory = inventoryService.selectByKey(takeStock.getId());
        if(inventory.getLibraryQuantity()<takeStock.getRealityLibraryQuantity()){
            omcInventory.setFollow(2);
            omcInventory.setLibraryQuantity(takeStock.getRealityLibraryQuantity());
        }
        if(inventory.getLibraryQuantity()>takeStock.getRealityLibraryQuantity()){
            omcInventory.setFollow(1);
            omcInventory.setLibraryQuantity(takeStock.getRealityLibraryQuantity());
        }else {
            omcInventory.setFollow(0);
        }

        omcInventory.setTheoryLibraryQuantity(inventory.getLibraryQuantity());

        //更改库存情况
        omcInventory.setOperator(getLoginAuthDto().getUserName());
        omcInventory.setCheckTime(new Date());
        int update = inventoryService.update(omcInventory);


        OmcInventory key = inventoryService.selectByKey(omcInventory.getId());
        //新增盘点记录
        OmcCheckRecord omcCheckRecord = new OmcCheckRecord();
        BeanUtils.copyProperties(key,omcCheckRecord);
        omcCheckRecord.setInventoryId(key.getId());
        omcCheckRecord.setId(null);
        int save = checkRecordService.save(omcCheckRecord);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询盘点记录")
    @RequestMapping(value = "/selectCheckRecord", method = RequestMethod.POST)
    public Wrapper selectCheckRecord(@RequestBody @Valid CheckRecordQueryDto checkRecordQueryDto)   {


        List<CheckRecordVo> select = checkRecordService.selectCheckRecord(checkRecordQueryDto);
        PageInfo<CheckRecordVo> pageInfo = new PageInfo<>(select);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
